package com.eb.config;


import com.eb.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
@CrossOrigin
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.eb.controller")) // 使用配置文件中的包名
                .paths(PathSelectors.any())  // 扫描所有路径
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .title("校园博客") // 使用配置文件中的标题
                        .description("接口文档") // 使用配置文件中的描述
                        .version("1.0") // 使用配置文件中的版本
                        .build());
    }
    /**
     * 设置静态资源映射
     * @param registry
     */

    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("设置静态资源映射");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始配置拦截器");
        registry.addInterceptor(new LoginInterceptor(stringRedisTemplate))
                .addPathPatterns("/user/**")
                .addPathPatterns("/innovation/**")
                .addPathPatterns("/comment/**")
                .addPathPatterns("/preserve/**")
                .addPathPatterns("/announcement/**")
                .addPathPatterns("/likes/**")
                .excludePathPatterns("/user/login",
                        "/user/register");
    }


}
